﻿using Abp.Authorization;
using Abp.Configuration.Startup;
using Abp.Localization;
using Abp.MultiTenancy;

namespace Magicodes.WeChat.Authorization
{
    /// <summary>
    /// 应用程序权限提供程序
    /// </summary>
    public class WeChatAuthorizationProvider : AuthorizationProvider
    {
        private readonly bool _isMultiTenancyEnabled;

        public WeChatAuthorizationProvider(bool isMultiTenancyEnabled)
        {
            _isMultiTenancyEnabled = isMultiTenancyEnabled;
        }

        public WeChatAuthorizationProvider(IMultiTenancyConfig multiTenancyConfig)
        {
            _isMultiTenancyEnabled = multiTenancyConfig.IsEnabled;
        }

        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            //定义权限根节点
            var root = context.GetPermissionOrNull(WeChatPermissions.Roots);

            #region WeChatUser
            var weChatUser = root.CreateChildPermission(WeChatPermissions.Roots_Tenants_WeChatUser, L("WeChatUser"));
            weChatUser.CreateChildPermission(WeChatPermissions.Roots_Tenants_WeChatUser_Create, L("CreatingNewWeChatUser"));
            weChatUser.CreateChildPermission(WeChatPermissions.Roots_Tenants_WeChatUser_Edit, L("EditingWeChatUser"));
            weChatUser.CreateChildPermission(WeChatPermissions.Roots_Tenants_WeChatUser_Delete, L("DeletingWeChatUser"));
            #endregion


            #region WeChatBind
            var weChatBind = root.CreateChildPermission(WeChatPermissions.Roots_Tenants_WeChatBind, L("WeChatBind"));
            weChatBind.CreateChildPermission(WeChatPermissions.Roots_Tenants_WeChatBind_Create, L("CreatingNewWeChatBind"));
            weChatBind.CreateChildPermission(WeChatPermissions.Roots_Tenants_WeChatBind_Edit, L("EditingWeChatBind"));
            weChatBind.CreateChildPermission(WeChatPermissions.Roots_Tenants_WeChatBind_Delete, L("DeletingWeChatBind"));
            #endregion

            root.CreateChildPermission(WeChatPermissions.Roots_Tenants_WeChatApiSetting, L("WeChatApiSetting"));
        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, WeChatConsts.LocalizationSourceName);
        }

    }
}